const express = require("express");
const router = express.Router();
const path = require("path");

const GoodService = require(path.join(process.cwd(), "/services/GoodService"));

router.get(
  "/",
  (req, res, next) => {
    if (!req.query.pagenum || req.query.pagenum <= 0)
      return res.sendResult(null, 400, "pagenum 参数错误");
    if (!req.query.pagesize || req.query.pagesize <= 0)
      return res.sendResult(null, 400, "pagesize 参数错误");
    next();
  },
  (req, res, next) => {
    const { query, pagenum, pagesize } = req.query;

    GoodService.getGoods({ query, pagenum, pagesize }, function (err, result) {
      if (err) return res.sendResult(400, null, err);
      res.sendResult(200, result, "获取成功");
    });
  }
);


//新增
router.post(
  "/",
  (req, res, next) => {
    next();
  },
  (req, res, next) => {
    const params = req.body;

    GoodService.createGood(params, function (err, result) {
      if (err) return res.sendResult(400, null, err);
      res.sendResult(201, result, "创建商品成功");
    });
  }
);

module.exports = router;
